package com.alisoft.nano.seda.notification;

import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;

import com.alisoft.nano.seda.Event;

@SuppressWarnings("serial")
public abstract class EventMachineNotificationListener<T> implements
		NotificationListener, NotificationFilter {
	
	@SuppressWarnings("unchecked")
	public void handleNotification(Notification notification, Object handback) {
		EventMachineNotification<T> eventMachineNotification = (EventMachineNotification<T>) notification;
		
		if (EventMachineNotification.ACCEPTED.equals(eventMachineNotification.getType())) {
			onAccepted(eventMachineNotification.getEvent());
		} else if (EventMachineNotification.ERROR.equals(eventMachineNotification.getType())) {
			onError(eventMachineNotification.getEvent());
		} else if (EventMachineNotification.COMPLETED.equals(eventMachineNotification.getType())) {
			onCompleted(eventMachineNotification.getEvent());
		} 
	}

	protected void onCompleted(Event<T> event) {
	}

	protected void onError(Event<T> event) {
	}

	protected void onAccepted(Event<T> event) {
	}

	public boolean isNotificationEnabled(Notification notification) {
		return EventMachineNotification.class.isAssignableFrom(notification.getClass());
	}
}
